
#include <string.h>
#include "ff.h"			/* Declarations of FatFs API */
#include "diskio.h"		/* Declarations of device I/O functions */
FATFS FatFs;   // Fatfs handle
FIL fil;       // File handle
FRESULT fr;    // FatFs return code
UINT br, bw;   // File read/write count

void init_FATFS(void)
{
    // Mount the file system
	  //FatFs.pdrv=1;
    fr = f_mount(&FatFs, "", 1);
    if (fr != FR_OK) {
        // Handle error
    }
}

void create_and_write_file(void)
{
    // Create a new file
    fr = f_open(&fil, "hello.txt", FA_CREATE_ALWAYS | FA_WRITE);
    if (fr == FR_OK) {
        // Write a line
        f_write(&fil, "Hello, STM32!", 13, &bw);
        
        // Close the file
        f_close(&fil);
    }
}

void read_file(void)
{
    // Open the existing file
    fr = f_open(&fil, "hello.txt", FA_READ);
    if (fr == FR_OK) {
        char buffer[64];
        f_read(&fil, buffer, sizeof(buffer), &br);
        
        // Use the read data
        f_close(&fil);
    }
}
